
//  QXImageView.swift
//  带进度条的图片
//
//  Created by QinXin on 15/7/6.
//  Copyright © 2015年 labi3285_Lab. All rights reserved.
//

import UIKit


class QXImageView: UIImageView {
    
    private lazy var progressView: QXProgressView = {
        let pv = QXProgressView()
        pv.backgroundColor = UIColor.clearColor()
        self.addSubview(pv)
        pv.ff_Fill(self)
        return pv
    }()
    
    var progress: CGFloat = 0 {
        didSet {
            if progress > 0 {
                progressView.progress = progress
            }
        }
    }
    
}

class QXProgressView: UIView {
    
    var progress: CGFloat = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    override func drawRect(rect: CGRect) {
        
        if progress == 0 || progress == 1 {
            return
        }
        
        let wh: CGFloat = 30.0
        let center = self.center
        
        let bgPath = UIBezierPath(ovalInRect: CGRect(x: center.x - wh * 0.5, y: center.y - wh * 0.5, width: wh, height: wh))
        UIColor(white: 0.0, alpha: 0.3).setFill()
        bgPath.fill()
        
        let proPath = UIBezierPath(arcCenter: center, radius: 15.0, startAngle: CGFloat(-M_PI_2), endAngle: 2.0 * CGFloat(M_PI * 0.5) * self.progress, clockwise: true)
        proPath.addLineToPoint(center)
        proPath.closePath()
        UIColor(white: 1.0, alpha: 0.5).setFill()
        proPath.fill()
        
    }
    
}
